package com.dmjj.common.md5;

import com.dmjj.common.constant.Constans;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Md5Util {

	/**
	 * 先拼字符串后缀再md5加密
	 * 
	 * @param code
	 * @return
	 */
	public static String md5Suffix(String code) {
		return md5Encrypt(code + Constans.SUFFIX);
	}

	/**
	 * 先md5加密，添加后缀再md5加密
	 * 
	 * @param code
	 * @return
	 */
	public static String md5No2(String code) {
		String md5Str = md5Encrypt(code);
		return md5Encrypt(md5Str + Constans.SUFFIX);
	}

	public static String md5Encrypt(String code) {
		MessageDigest md5 = null;
		try {
			md5 = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		char[] charArray = code.toCharArray();
		byte[] byteArray = new byte[charArray.length];
		for (int i = 0; i < charArray.length; i++) {
			byteArray[i] = (byte) charArray[i];
		}
		byte[] md5Bytes = md5.digest(byteArray);
		StringBuffer hexValue = new StringBuffer();
		for (int i = 0; i < md5Bytes.length; i++) {
			int val = ((int) md5Bytes[i]) & 0xff;
			if (val < 16) {
				hexValue.append("0");
			}
			hexValue.append(Integer.toHexString(val));
		}
		return hexValue.toString();
	}

	/**
	 * MD5加密算法二,先进行一次加密,再拼接后缀进行二次加密
	 * 
	 * @param code
	 * @return
	 */
	public static String getMd5No2(String code) {
		String md5Str = getMd5Encode(code);
		return getMd5Encode(md5Str + Constans.SUFFIX);
	}

	/**
	 * MD5加密算法三
	 * 
	 * @param text
	 * @return
	 */
	public static String getMd5Encode(String text) {
		try {
			MessageDigest digest = MessageDigest.getInstance("MD5");
			byte[] result = digest.digest(text.getBytes());
			StringBuilder sb = new StringBuilder();
			for (byte b : result) {
				int number = b & 0xff;
				String hex = Integer.toHexString(number);
				if (hex.length() == 1) {
					sb.append("0").append(hex);
				} else {
					sb.append(hex);
				}
			}
			return sb.toString();
		} catch (NoSuchAlgorithmException e) {
			return "";
		}
	}

    /**
     * MD5加密算法四,先进行一次加密,再拼接后缀
     *
     * @param code
     * @return
     */
    public static String getMd5No4(String code) {
        String md5Str = getMd5Encode(code);
        String resuleStr = md5Str + Constans.SUFFIX;
        return resuleStr;
    }

	/*public static void main(String[] args) {
		String md5Str = Md5Util.getMd5Encode("1");
		System.out.println(md5Str);
	}*/

	public static String getSha1(String str){
		if(str==null||str.length()==0){
			return null;
		}
		char hexDigits[] = {'0','1','2','3','4','5','6','7','8','9',
				'a','b','c','d','e','f'};
		try {
			MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
			mdTemp.update(str.getBytes("UTF-8"));

			byte[] md = mdTemp.digest();
			int j = md.length;
			char buf[] = new char[j*2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
				buf[k++] = hexDigits[byte0 & 0xf];
			}
			String pwdstr = new String(buf);
			return pwdstr.toUpperCase();
		} catch (Exception e) {
			return null;
		}
	}

}
